package org.ihelpuoo.orm.handler.conn;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;

import org.ihelpuoo.lang.ObjectUtils;
import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.orm.filter.FieldFilter;
import org.ihelpuoo.orm.handler.ObjectProcessor;
import org.ihelpuoo.orm.handler.FieldHandler;

public class ConnSaveObjectFieldHandler extends FieldHandler
{

	public ConnSaveObjectFieldHandler(ObjectProcessor beanHandler)
	{
		super(beanHandler);
	}

	@Override
	public void processWithoutExceptionHandle(ObjectRule r, Field f, Object conn, Object obj, FieldFilter actived, FieldFilter locked, String prefix)
			throws Exception
	{
		Method getter = ObjectUtils.getGetter(obj.getClass(),f);
		Object sub = getter.invoke(obj,(Object[])null);
		if (sub == null)
			return;
		ObjectRule subRule = processor.getManual().getRule(sub.getClass());
		subRule.checkDatabaseConfig((Connection)conn,sub);
		if (subRule.isNewInDB(sub))
			processor.insert(subRule,conn,sub,null,null);
		else
			processor.update(subRule,conn,sub,null,null);
	}

}
